package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcFlowMessageGroupMapper;
import com.csun.cmny.provider.dao.OmcFlowMessageMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcCurTaskQuery;
import com.csun.cmny.provider.model.domain.OmcFlowMessage;
import com.csun.cmny.provider.model.domain.OmcFlowMessageGroup;
import com.csun.cmny.provider.model.dto.message.FlowMessageQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcFlowMessageService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-28 10:23
 * @Description:
 */
@Service
@Transactional
public class OmcFlowMessageServiceImpl extends BaseService<OmcFlowMessage>
        implements OmcFlowMessageService {

    @Resource
    private OmcFlowMessageMapper omcFlowMessageMapper;
    @Resource
    private OmcFlowMessageGroupMapper omcFlowMessageGroupMapper;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * add omc flow message
     *
     * @param omcFlowMessage the omc flow message
     * @param loginAuthDto   the login auth dto
     * @return the int
     */
    @Override
    public Integer add(OmcFlowMessage omcFlowMessage, LoginAuthDto loginAuthDto) {

        OmcFlowMessage omcFlowMessageQuery = new OmcFlowMessage();
        omcFlowMessageQuery.setCode(omcFlowMessage.getCode());
        int count = omcFlowMessageMapper.selectCount(omcFlowMessageQuery);
        if (count > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031401);
        }

        omcFlowMessage.setUpdateInfo(loginAuthDto);

        return omcFlowMessageMapper.insertSelective(omcFlowMessage);
    }

    /**
     * edit omc flow message
     *
     * @param omcFlowMessage the omc flow message
     * @param loginAuthDto   the login auth dto
     * @return the int
     */
    @Override
    public Integer edit(OmcFlowMessage omcFlowMessage, LoginAuthDto loginAuthDto) {

        // id 不能为空
        Preconditions.checkArgument(PublicUtil.isNotEmpty(omcFlowMessage.getId()),
                cmnyMessageSource.getMessage("flow.message.id.null", null));

        OmcFlowMessage omcFlowMessageQuery = omcFlowMessageMapper.selectByPrimaryKey(omcFlowMessage.getId());
        if (PublicUtil.isEmpty(omcFlowMessageQuery)) {
            //
            throw new OmcBizException(ErrorCodeEnum.OMC10031402);
        }

        // code 是否冲突
        if (PublicUtil.isNotEmpty(omcFlowMessage.getCode())) {
            omcFlowMessageQuery = new OmcFlowMessage();
            omcFlowMessageQuery.setCode(omcFlowMessage.getCode());
            omcFlowMessageQuery = omcFlowMessageMapper.selectOne(omcFlowMessageQuery);
            if (omcFlowMessageQuery != null && !omcFlowMessageQuery.getId().equals(omcFlowMessage.getId())) {
                throw new OmcBizException(ErrorCodeEnum.OMC10031401);
            }
        }

        omcFlowMessage.setUpdateInfo(loginAuthDto);

        return omcFlowMessageMapper.updateByPrimaryKeySelective(omcFlowMessage);
    }

    /**
     * delete by id
     *
     * @param id the omc flow message id
     * @return the int
     */
    @Override
    public Integer deleteById(Long id) {

        // 判断已经配置
        OmcFlowMessageGroup omcFlowMessageGroup = new OmcFlowMessageGroup();
        omcFlowMessageGroup.setFlowMessageId(id);
        int count = omcFlowMessageGroupMapper.selectCount(omcFlowMessageGroup);
        if (count > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031404);
        }

        return omcFlowMessageMapper.deleteByPrimaryKey(id);
    }

    /**
     * query omc flow message list with page
     *
     * @param flowMessageQueryDto the flow message query dto
     *
     * @return the list.
     */
    @Override
    public List<OmcFlowMessage> queryListWithPage(FlowMessageQueryDto flowMessageQueryDto) {

        PageHelper.startPage(flowMessageQueryDto.getPageNum(), flowMessageQueryDto.getPageSize());

        return omcFlowMessageMapper.queryListWithPage(flowMessageQueryDto);
    }
}
